﻿Imports System.Data
Imports System.Data.SqlClient
Imports Sistema_InformacionNaviera.BE
Imports Sistema_InformacionNavier.UTIL

Public Class DiasLibresDALC
    Public Function ListaDiasLibres(ByVal objDiaslibres As DiasLibresBE) As List(Of DiasLibresBE)

        Dim sqlListar As String
        Dim objListaDLibres As New List(Of DiasLibresBE)
        Dim objListaDLibresBE As DiasLibresBE = Nothing
        Dim arrSqlParameter(2) As SqlParameter
        Dim ds As DataSet
        Dim dt As DataTable

        Dim objDatos As New AccesoDatos()
        Dim blnExito As Boolean
        Dim strMensaje As String = String.Empty
        Dim intI As Integer = 0

        Try

            sqlListar = "DiasLibres_Listar"

            arrSqlParameter(0) = New SqlParameter("@Serv", System.Data.SqlDbType.VarChar)
            arrSqlParameter(0).Value = objDiaslibres.Serv
            objListaDLibres = New List(Of DiasLibresBE)()

            arrSqlParameter(1) = New SqlParameter("@CodCli", System.Data.SqlDbType.VarChar)
            arrSqlParameter(1).Value = objDiaslibres.CodCli
            objListaDLibres = New List(Of DiasLibresBE)()

            arrSqlParameter(2) = New SqlParameter("@Esta", System.Data.SqlDbType.VarChar)
            arrSqlParameter(2).Value = objDiaslibres.Esta
            objListaDLibres = New List(Of DiasLibresBE)()


            ds = objDatos.DevuelveDataSet(sqlListar, blnExito, strMensaje, arrSqlParameter)
            dt = ds.Tables(0)

            If dt.Rows.Count > 0 Then

                While intI < dt.Rows.Count
                    objListaDLibresBE = New DiasLibresBE()

                    objListaDLibresBE.Tipo = dt.Rows(intI).ItemArray(0).ToString
                    objListaDLibresBE.CodCli = dt.Rows(intI).ItemArray(1).ToString
                    objListaDLibresBE.DesCli = dt.Rows(intI).ItemArray(2).ToString
                    objListaDLibresBE.CodEmb = dt.Rows(intI).ItemArray(3).ToString
                    objListaDLibresBE.DesEmb = dt.Rows(intI).ItemArray(4).ToString
                    objListaDLibresBE.Kdli = dt.Rows(intI).ItemArray(5).ToString
                    objListaDLibresBE.NAcuerdo = dt.Rows(intI).ItemArray(6).ToString
                    objListaDLibresBE.Dias = dt.Rows(intI).ItemArray(7).ToString
                    objListaDLibresBE.AplHijo = dt.Rows(intI).ItemArray(8).ToString
                    objListaDLibresBE.IniVig = dt.Rows(intI).ItemArray(9).ToString
                    objListaDLibresBE.FinVig = dt.Rows(intI).ItemArray(10).ToString
                    objListaDLibresBE.Serv = dt.Rows(intI).ItemArray(11).ToString
                    objListaDLibresBE.Ucre = dt.Rows(intI).ItemArray(12).ToString
                    objListaDLibresBE.FCre = dt.Rows(intI).ItemArray(13).ToString
                    objListaDLibresBE.UMod = dt.Rows(intI).ItemArray(14).ToString
                    objListaDLibresBE.FMod = dt.Rows(intI).ItemArray(15).ToString
                    objListaDLibresBE.Prio = dt.Rows(intI).ItemArray(16).ToString
                    intI = intI + 1

                    objListaDLibres.Add(objListaDLibresBE)
                End While
            End If
        Catch ex As Exception
            MsgBox(ex.Message, vbCritical, "Error del sistema")
            Return Nothing
            Exit Function
        End Try

        Return objListaDLibres
    End Function


    Public Function ListaDiasLibEdicion(ByVal objDiaslibres As DiasLibresBE) As DiasLibresBE

        Dim sqlListar As String
        'Dim objListaDLibres As New List(Of DiasLibresBE)
        Dim objListaDLibresBE As DiasLibresBE = Nothing
        Dim arrSqlParameter(0) As SqlParameter
        Dim ds As DataSet
        Dim dt As DataTable

        Dim objDatos As New AccesoDatos()
        Dim blnExito As Boolean
        Dim strMensaje As String = String.Empty
        Dim intI As Integer = 0

        Try

            sqlListar = "DiasLibres_ListaEdicion"

            arrSqlParameter(0) = New SqlParameter("@KDLI", System.Data.SqlDbType.VarChar)
            arrSqlParameter(0).Value = objDiaslibres.Kdli 

            ds = objDatos.DevuelveDataSet(sqlListar, blnExito, strMensaje, arrSqlParameter)
            dt = ds.Tables(0)

            If dt.Rows.Count > 0 Then

                'While intI < dt.Rows.Count
                objListaDLibresBE = New DiasLibresBE()

                objListaDLibresBE.Tipo = dt.Rows(intI).ItemArray(0).ToString
                objListaDLibresBE.CodCli = dt.Rows(intI).ItemArray(1).ToString
                objListaDLibresBE.DesCli = dt.Rows(intI).ItemArray(2).ToString
                objListaDLibresBE.CodEmb = dt.Rows(intI).ItemArray(3).ToString
                objListaDLibresBE.DesEmb = dt.Rows(intI).ItemArray(4).ToString
                objListaDLibresBE.Kdli = dt.Rows(intI).ItemArray(5).ToString
                objListaDLibresBE.NAcuerdo = dt.Rows(intI).ItemArray(6).ToString
                objListaDLibresBE.Dias = dt.Rows(intI).ItemArray(7).ToString
                objListaDLibresBE.AplHijo = dt.Rows(intI).ItemArray(8).ToString
                objListaDLibresBE.IniVig = dt.Rows(intI).ItemArray(9).ToString
                objListaDLibresBE.FinVig = dt.Rows(intI).ItemArray(10).ToString
                objListaDLibresBE.Serv = dt.Rows(intI).ItemArray(11).ToString
                objListaDLibresBE.Ucre = dt.Rows(intI).ItemArray(12).ToString
                objListaDLibresBE.FCre = dt.Rows(intI).ItemArray(13).ToString
                objListaDLibresBE.UMod = dt.Rows(intI).ItemArray(14).ToString
                objListaDLibresBE.FMod = dt.Rows(intI).ItemArray(15).ToString
                objListaDLibresBE.Prio = dt.Rows(intI).ItemArray(16).ToString
                ' intI = intI + 1

                'objListaDLibres.Add(objListaDLibresBE)
                'End While
            End If
        Catch ex As Exception
            MsgBox(ex.Message, vbCritical, "Error del sistema")
            Return Nothing
            Exit Function
        End Try

        Return objListaDLibresBE
    End Function

    Public Function InsertarDiasLibres(ByVal objDLibresBE As DiasLibresBE) As Integer
        Dim objDatos As New AccesoDatos()

        Dim ds As DataSet
        Dim dt As DataTable
        Dim arrSqlParameter(8) As SqlParameter
        Dim iKacu As Integer = 0
        Dim blnExito As Boolean
        Dim strMensaje As String = String.Empty
        Try

            arrSqlParameter(0) = New SqlParameter()
            arrSqlParameter(0).ParameterName = "@Serv"
            arrSqlParameter(0).SqlDbType = SqlDbType.VarChar
            arrSqlParameter(0).Size = 3
            arrSqlParameter(0).Direction = ParameterDirection.Input
            arrSqlParameter(0).Value = objDLibresBE.Serv

            arrSqlParameter(1) = New SqlParameter()
            arrSqlParameter(1).ParameterName = "@Clie"
            arrSqlParameter(1).SqlDbType = SqlDbType.VarChar
            arrSqlParameter(1).Size = 6
            arrSqlParameter(1).Direction = ParameterDirection.Input
            arrSqlParameter(1).Value = objDLibresBE.CodCli

            arrSqlParameter(2) = New SqlParameter()
            arrSqlParameter(2).ParameterName = "@Emb"
            arrSqlParameter(2).SqlDbType = SqlDbType.VarChar
            arrSqlParameter(2).Size = 6
            arrSqlParameter(2).Direction = ParameterDirection.Input
            arrSqlParameter(2).Value = objDLibresBE.CodEmb

            arrSqlParameter(3) = New SqlParameter()
            arrSqlParameter(3).ParameterName = "@Nacu"
            arrSqlParameter(3).SqlDbType = SqlDbType.VarChar
            arrSqlParameter(3).Size = 15
            arrSqlParameter(3).Direction = ParameterDirection.Input
            arrSqlParameter(3).Value = objDLibresBE.NAcuerdo

            arrSqlParameter(4) = New SqlParameter()
            arrSqlParameter(4).ParameterName = "@Fiving"
            arrSqlParameter(4).SqlDbType = SqlDbType.VarChar
            arrSqlParameter(4).Size = 10
            arrSqlParameter(4).Direction = ParameterDirection.Input
            arrSqlParameter(4).Value = objDLibresBE.IniVig

            arrSqlParameter(5) = New SqlParameter()
            arrSqlParameter(5).ParameterName = "@Ffvig"
            arrSqlParameter(5).SqlDbType = SqlDbType.VarChar
            arrSqlParameter(5).Size = 10
            arrSqlParameter(5).Direction = ParameterDirection.Input
            arrSqlParameter(5).Value = objDLibresBE.FinVig

            arrSqlParameter(6) = New SqlParameter()
            arrSqlParameter(6).ParameterName = "@Grp"
            arrSqlParameter(6).SqlDbType = SqlDbType.Int
            arrSqlParameter(6).Direction = ParameterDirection.Input
            arrSqlParameter(6).Value = objDLibresBE.Grp

            arrSqlParameter(7) = New SqlParameter()
            arrSqlParameter(7).ParameterName = "@Ucre"
            arrSqlParameter(7).SqlDbType = SqlDbType.VarChar
            arrSqlParameter(7).Size = 15
            arrSqlParameter(7).Direction = ParameterDirection.Input
            arrSqlParameter(7).Value = objDLibresBE.Ucre

            arrSqlParameter(8) = New SqlParameter()
            arrSqlParameter(8).ParameterName = "@AplicaHijo"
            arrSqlParameter(8).SqlDbType = SqlDbType.Int
            arrSqlParameter(8).Direction = ParameterDirection.Input
            arrSqlParameter(8).Value = objDLibresBE.AplHijo
            Dim oDatos As New AccesoDatos()

            ds = objDatos.DevuelveDataSet("DiasLibres_Insertar", blnExito, strMensaje, arrSqlParameter)
            dt = ds.Tables(0)
            iKacu = 0
            If dt.Rows.Count > 0 Then
                iKacu = dt.Rows(0).Item(0).ToString
            End If
            Return iKacu

        Catch ex As Exception
            Throw
        End Try

    End Function

    Public Function ModificarDiasLibres(ByVal objDLibresBE As DiasLibresBE) As Boolean
        Dim objDatos As New AccesoDatos()

        Dim ds As DataSet
        Dim arrSqlParameter(5) As SqlParameter
        Dim iKacu As Integer = 0
        Dim blnExito As Boolean
        Dim strMensaje As String = String.Empty
        Try

            arrSqlParameter(0) = New SqlParameter()
            arrSqlParameter(0).ParameterName = "@KAcu"
            arrSqlParameter(0).SqlDbType = SqlDbType.Int
            arrSqlParameter(0).Direction = ParameterDirection.Input
            arrSqlParameter(0).Value = objDLibresBE.Kdli

            arrSqlParameter(1) = New SqlParameter()
            arrSqlParameter(1).ParameterName = "@Fiving"
            arrSqlParameter(1).SqlDbType = SqlDbType.VarChar
            arrSqlParameter(1).Size = 10
            arrSqlParameter(1).Direction = ParameterDirection.Input
            arrSqlParameter(1).Value = objDLibresBE.IniVig

            arrSqlParameter(2) = New SqlParameter()
            arrSqlParameter(2).ParameterName = "@Ffvig"
            arrSqlParameter(2).SqlDbType = SqlDbType.VarChar
            arrSqlParameter(2).Size = 10
            arrSqlParameter(2).Direction = ParameterDirection.Input
            arrSqlParameter(2).Value = objDLibresBE.FinVig

            arrSqlParameter(3) = New SqlParameter()
            arrSqlParameter(3).ParameterName = "@Grp"
            arrSqlParameter(3).SqlDbType = SqlDbType.Int
            arrSqlParameter(3).Direction = ParameterDirection.Input
            arrSqlParameter(3).Value = objDLibresBE.Grp

            arrSqlParameter(4) = New SqlParameter()
            arrSqlParameter(4).ParameterName = "@Ucre"
            arrSqlParameter(4).SqlDbType = SqlDbType.VarChar
            arrSqlParameter(4).Size = 15
            arrSqlParameter(4).Direction = ParameterDirection.Input
            arrSqlParameter(4).Value = objDLibresBE.Ucre

            arrSqlParameter(5) = New SqlParameter()
            arrSqlParameter(5).ParameterName = "@AplicaHijo"
            arrSqlParameter(5).SqlDbType = SqlDbType.Int
            arrSqlParameter(5).Direction = ParameterDirection.Input
            arrSqlParameter(5).Value = objDLibresBE.AplHijo
            Dim oDatos As New AccesoDatos()

            ds = objDatos.DevuelveDataSet("DiasLibres_Modificar", blnExito, strMensaje, arrSqlParameter)
           
            
            Return blnExito

        Catch ex As Exception
            Throw
        End Try

    End Function


    Public Function EliminarDiasLibres(ByVal objDiasLibresBE As DiasLibresBE, ByRef bExito As Boolean, ByRef sMensaje As String) As Boolean

        Dim arrSqlParameter(1) As SqlParameter

        Try

            arrSqlParameter(0) = New SqlParameter()
            arrSqlParameter(0).ParameterName = "@KAcu"
            arrSqlParameter(0).SqlDbType = SqlDbType.Int
            arrSqlParameter(0).Value = objDiasLibresBE.Kdli

            arrSqlParameter(1) = New SqlParameter()
            arrSqlParameter(1).ParameterName = "@TIPO"
            arrSqlParameter(1).SqlDbType = SqlDbType.VarChar
            arrSqlParameter(1).Value = objDiasLibresBE.Tipo

            Dim oDatos As New AccesoDatos()

            oDatos.EjecutaSP("DiasLibres_Eliminar", bExito, sMensaje, arrSqlParameter)

            Return bExito

        Catch ex As Exception
            MsgBox(ex.Message, vbCritical, "Error del sistema")
            Return False
            Exit Function
        End Try

    End Function

   End Class
